<?php
class ChannelStore extends KF_Auth_Controller
{
    
    /*
     * 门店列表页
     * */
    public function index()
    {        
        $this->load->model("Common_model", "CommonModel");
        $CommonModel = $this->CommonModel;

        $channel = $CommonModel->getAllChannelInfo();
        $PTID = $channel[1]['id'];
        if(empty($_GET['id']) == false){
            $PTID = $_GET['paltforms'];
        }
        if(IS_POST){
            $where = array();
            $config = array(
                'table' => 'okdkyao.dk_app_yaodian',
                'columns' => array(
                    array('db' => 'ID'),
                    array('db' => 'Caption'),
                    array('db' => 'Address')
                ),
                'where' => implode(' and ', $where)
            );
            $this->load->library('dataTables', $config);
            $result = $this->datatables->search();
            
            exit(json_encode($result));
        }
        $this->load->view("channel_store/index",array('channel'=>$channel,'ptid'=>$PTID));
    }

    /*
    *   通过药店ID查配送平台信息
     */
    public function getPtInfoByYid()
    {
        header('Content-type: application/json');
        $store_id = $_GET['store_id']; 

        //配送平台默认状态列表
        $carrierList = $this->getCarrierDefaultStatusList();

        $this->load->model("ChannelStore_model", "ChannelStoreModel");
        $ChannelStoreModel = $this->ChannelStoreModel;

        $data = $ChannelStoreModel->getStoreStatusByYid($store_id);

    	foreach ($carrierList as $key => $value) {
    		$carrierList[$key]['store_status'] = 0;
    	}
    	foreach ($data as $key => $value) {
            $carrierList[$value['fk_channel_id']]['store_status'] = $value['store_status'];

        }
         
        $carrierList = array_values($carrierList);
        echo json_encode($carrierList);
    }

    /*
        获取配送平台默认状态列表
    */
    public function getCarrierDefaultStatusList($channel_id = 0)
    {
        $this->load->model("Common_model", "CommonModel");
        $CommonModel = $this->CommonModel;

        $channel = $CommonModel->getAllChannelInfo($channel_id);
        return $channel;
    }

    /*
    *   通过药店ID和配送平台ID查看配送平台状态
     */
    public function getPtStatusByYid()
    {        
        $store_id = $_GET['YID']; 
        $ptid = $_GET['PTID'];

        $this->load->model("ChannelStore_model", "ChannelStoreModel");
        $ChannelStoreModel = $this->ChannelStoreModel;

        $data = $ChannelStoreModel->getStoreStatusById($ptid, $store_id);

        if(empty($data))
        {
        	$this->load->model("Common_model", "CommonModel");
        	$CommonModel = $this->CommonModel;

        	$channel_info = $CommonModel->getAllChannelInfo($ptid);
        	$res['name'] = $channel_info['name'];

        	$storeInfo = $CommonModel->getStoreInfoByStoreId($store_id);
        	
        	//创建门店
	        switch ($ptid) {
	            case 1:  //美团
	                $storeInfo['create_url'] = '/ChannelStore/mtCreate'; 
	                $storeInfo['chn_id'] = 1; 
	                break;
	            case 2: //百度
	                $storeInfo['create_url'] = '/ChannelStore/bdCreate';
	                $storeInfo['chn_id'] = 2; 
	                break;
	            case 3:  //京东
	                $storeInfo['create_url'] = '/ChannelStore/jdCreate';
	                $storeInfo['chn_id'] = 3; 
	                break;
	            case 4:  //饿了么
	                $storeInfo['create_url'] = '/ChannelStore/eleCreate';
	                $storeInfo['chn_id'] = 4; 
	                break;
	            default:
	                # code...
	                break;
	        }

	        $res['data'] = $storeInfo;

        	$this->load->view("channel_store/store_baseinfo",$res);
        	return;
        }

        $pt_data = $this->getCarrierDefaultStatusList($ptid);
        $data['PTNAME'] = $pt_data['name'];//配送平台名称
        $data['PTID'] = $pt_data['id'];//配送平台ID

    	$result['data'] = $data;
        $this->editChannelStore($ptid, $store_id);
    }

    /*
    *   通过药店ID和配送平台ID修改配送平台状态
     */
    public function updatePtStatusByYid()
    {
        $store_id = $_POST['store_id']; 
        $ptid = $_POST['ptid']; 
        $status = $_POST['zt'];
        $where['fk_store_id'] = $store_id;
        $where['fk_channel_id'] = $ptid;
        $data['store_status'] = $status;

        $res = $this->db->update('kfsy_channel_store.kf_channel_store',$data,$where);

        if($res)
        {
            echo json_encode(array('code'=>0 ,'msg'=>'修改成功'));
        }else
        {
            echo json_encode(array('code'=>-1 ,'msg'=>'修改失败'));
        }
    }

    //创建美团门店
   	public function mtCreate()
   	{
   		$store_id = $_POST['store_id'];
        $chn_store_id = $_POST['chn_store_id'];
   		$store_name = $_POST['store_name'];
   		$start_time = strtotime($_POST['start_time']) - strtotime('00:00:00');
   		$end_time = strtotime($_POST['end_time']) - strtotime('00:00:00');

   		$this->load->library('AdminApi/MtApi', '', 'MtApi');
        $MtApi = $this->MtApi;

        $res = $MtApi->addStoreInfo($store_id, $store_name, $start_time, $end_time, $chn_store_id);
        echo json_encode($res);
   	}

   	//创建百度门店
   	public function bdCreate()
   	{
   		$store_id = $_POST['store_id'];
   		$store_name = $_POST['store_name'];
   		$start_time = strtotime($_POST['start_time']) - strtotime('00:00:00');
   		$end_time = strtotime($_POST['end_time']) - strtotime('00:00:00');

   		$this->load->library('AdminApi/BdApi', '', 'BdApi');
        $BdApi = $this->BdApi;

        $res = $BdApi->addStoreInfo($store_id, $store_name, $start_time, $end_time);
        echo json_encode($res);
   	}

   	//创建京东门店
   	public function jdCreate()
   	{
        $store_id = $_POST['store_id'];
        $store_name = $_POST['store_name'];
        $start_time = strtotime($_POST['start_time']) - strtotime('00:00:00');
        $end_time = strtotime($_POST['end_time']) - strtotime('00:00:00');

        $this->load->library('AdminApi/JddjApi', '', 'JddjApi');
        $JddjApi = $this->JddjApi;

        $res = $JddjApi->addStoreInfo($store_id, $store_name, $start_time, $end_time);
        echo json_encode($res);
   	}

   	//创建饿了么门店
   	public function eleCreate()
   	{
        $store_id = $_POST['store_id'];
        $chn_store_id = $_POST['chn_store_id'];
        $store_name = $_POST['store_name'];
        $start_time = strtotime($_POST['start_time']) - strtotime('00:00:00');
        $end_time = strtotime($_POST['end_time']) - strtotime('00:00:00');

        $this->load->library('AdminApi/ElemeApi', '', 'ElemeApi');
        $ElemeApi = $this->ElemeApi;

        $res = $ElemeApi->addStoreInfo($store_id, $chn_store_id);
        echo json_encode($res);
   	}

    //编辑门店
    public function editChannelStore($chn_id = 0, $store_id = 0)
    {
        $this->load->model("Common_model", "CommonModel");
        $CommonModel = $this->CommonModel;

        $channel_info = $CommonModel->getChannelStoreInfoById($chn_id, $store_id);

        $channel = $CommonModel->getAllChannelInfo($chn_id);
        $chn_name = $channel['name'];

        $shipping_start_time = $channel_info['shipping_start_time'] + strtotime('00:00:00');
        $channel_info['shipping_start_time'] = date('H:i:s',$shipping_start_time);
        $shipping_end_time = $channel_info['shipping_end_time'] + strtotime('00:00:00');
        $channel_info['shipping_end_time'] = date('H:i:s',$shipping_end_time);

        $channel_info['chn_id'] = $chn_id;
        $channel_info['store_id'] = $store_id;

        $result['data'] = $channel_info;
        $result['chn_name'] = $chn_name;

        $this->load->view("channel_store/edit_channel_store",$result);
    }
    //修改门店
    public function updateChannelStore()
    {
        $chn_id = $_POST['chn_id'];
        $store_id = $_POST['store_id'];
        $other_shop_name = $_POST['other_shop_name'];
        $other_shop_desc = $_POST['other_shop_desc'];
        $start_time = $_POST['start_time'];
        $end_time = $_POST['end_time'];
        $shipping_fee = $_POST['shipping_fee'];
        $store_status = $_POST['store_status'];

        $this->load->model("ChannelStore_model", "ChannelStoremodel");
        $ChannelStoremodel = $this->ChannelStoremodel;

        $res = $ChannelStoremodel->updateChannelStore($chn_id, $store_id, $other_shop_name, $other_shop_desc, $start_time, $end_time, $shipping_fee, $store_status);

        echo json_encode($res);
    }


}